const Controller = require('egg').Controller
const path = require('path')
const fs = require('fs')
class UploadController extends Controller {
	/**
	 * 1. npm i egg-oss
	 *
	 * 2. 配置 oss / config.default.js (1. 配置文件解析 2. 配置基本oss信息)
	 *
	 * ctx.oss.put
	 *
	 * ctx.oss.delete
	 *
	 * ctx.oss.get
	 *
	 */
	// 上传
	async upload() {
		const file = this.ctx.request.files[0]
		let pathName =
			'/1901A-egg-oss-imgs/' + Math.random() * 100 + path.extname(file.filename)
		const data = await this.ctx.oss.put(pathName, file.filepath)
		this.ctx.body = data
	}
	// 删除
	async delete() {
		// ctx.oss.delete('图片名字')
		// const {}
		const data = await this.ctx.oss.delete(
			'1901A-egg-oss-imgs/36.041536484098.jpeg'
		)
		this.ctx.body = data
	}
	// 下载
	async download() {
		const { filePath } = this.ctx.request.body
		const data = await this.ctx.oss.get(
			filePath,
			path.join(
				__dirname,
				`../public/imgs/${Math.random() * 100}_${path.extname(filePath)}`
			)
		)
		this.ctx.body = data
	}
}

module.exports = UploadController
